package com.ubb.dbms.core.parser.visitors;

import net.sf.jsqlparser.expression.AllComparisonExpression;
import net.sf.jsqlparser.expression.AnyComparisonExpression;
import net.sf.jsqlparser.expression.BinaryExpression;
import net.sf.jsqlparser.expression.CaseExpression;
import net.sf.jsqlparser.expression.DateValue;
import net.sf.jsqlparser.expression.DoubleValue;
import net.sf.jsqlparser.expression.ExpressionVisitor;
import net.sf.jsqlparser.expression.Function;
import net.sf.jsqlparser.expression.InverseExpression;
import net.sf.jsqlparser.expression.JdbcParameter;
import net.sf.jsqlparser.expression.LongValue;
import net.sf.jsqlparser.expression.NullValue;
import net.sf.jsqlparser.expression.Parenthesis;
import net.sf.jsqlparser.expression.StringValue;
import net.sf.jsqlparser.expression.TimeValue;
import net.sf.jsqlparser.expression.TimestampValue;
import net.sf.jsqlparser.expression.WhenClause;
import net.sf.jsqlparser.expression.operators.arithmetic.Addition;
import net.sf.jsqlparser.expression.operators.arithmetic.BitwiseAnd;
import net.sf.jsqlparser.expression.operators.arithmetic.BitwiseOr;
import net.sf.jsqlparser.expression.operators.arithmetic.BitwiseXor;
import net.sf.jsqlparser.expression.operators.arithmetic.Concat;
import net.sf.jsqlparser.expression.operators.arithmetic.Division;
import net.sf.jsqlparser.expression.operators.arithmetic.Multiplication;
import net.sf.jsqlparser.expression.operators.arithmetic.Subtraction;
import net.sf.jsqlparser.expression.operators.conditional.AndExpression;
import net.sf.jsqlparser.expression.operators.conditional.OrExpression;
import net.sf.jsqlparser.expression.operators.relational.Between;
import net.sf.jsqlparser.expression.operators.relational.EqualsTo;
import net.sf.jsqlparser.expression.operators.relational.ExistsExpression;
import net.sf.jsqlparser.expression.operators.relational.GreaterThan;
import net.sf.jsqlparser.expression.operators.relational.GreaterThanEquals;
import net.sf.jsqlparser.expression.operators.relational.InExpression;
import net.sf.jsqlparser.expression.operators.relational.IsNullExpression;
import net.sf.jsqlparser.expression.operators.relational.LikeExpression;
import net.sf.jsqlparser.expression.operators.relational.Matches;
import net.sf.jsqlparser.expression.operators.relational.MinorThan;
import net.sf.jsqlparser.expression.operators.relational.MinorThanEquals;
import net.sf.jsqlparser.expression.operators.relational.NotEqualsTo;
import net.sf.jsqlparser.schema.Column;
import net.sf.jsqlparser.statement.select.SubSelect;

import com.ubb.dbms.domain.queryData.Condition;
import com.ubb.dbms.domain.queryData.Operator;

public class SQLConditionVisitor implements ExpressionVisitor {
	
	private Condition condition;
	
	private boolean isLeftOperanndSet = false;
	
	public SQLConditionVisitor() {
		condition = new Condition();
	}
	
	public void visitBinaryExpression(BinaryExpression binaryExpression) {
		binaryExpression.getLeftExpression().accept(this);
		isLeftOperanndSet = true;
		binaryExpression.getRightExpression().accept(this);
	}
	
	public void setOperand(String operand) {
		if (isLeftOperanndSet)
			condition.setRightOperand(operand);
		else
			condition.setLeftOperand(operand);
	}

	@Override
	public void visit(EqualsTo equalsTo) {
		condition.setOperator(Operator.EQUAL);
		visitBinaryExpression(equalsTo);
	}
	
	@Override
	public void visit(NotEqualsTo notEqualsTo) {
		condition.setOperator(Operator.NOT_EQUAL);
		visitBinaryExpression(notEqualsTo);
	}
	
	@Override
	public void visit(GreaterThan greaterThan) {
		condition.setOperator(Operator.GREATHER);
		visitBinaryExpression(greaterThan);
	}
	
	@Override
	public void visit(GreaterThanEquals greaterThanEquals) {
		condition.setOperator(Operator.GREATHER_OR_EQUAL);
		visitBinaryExpression(greaterThanEquals);
	}
	
	@Override
	public void visit(MinorThan minorThan) {
		condition.setOperator(Operator.LOWER);
		visitBinaryExpression(minorThan);
	}

	@Override
	public void visit(MinorThanEquals minorThanEquals) {
		condition.setOperator(Operator.LOWER_OR_EQUAL);
		visitBinaryExpression(minorThanEquals);
	}
	
	@Override
	public void visit(LikeExpression likeExpression) {
		condition.setOperator(Operator.EQUAL);
		visitBinaryExpression(likeExpression);
	}
	
	@Override
	public void visit(DoubleValue doubleValue) {
		setOperand(doubleValue.toString());
//		System.out.println("DoubleValue Visitor");
	}

	@Override
	public void visit(LongValue longValue) {
		setOperand(longValue.toString());
//		System.out.println("LongValue Visitor");
	}

	@Override
	public void visit(DateValue dateValue) {
		setOperand(dateValue.toString());
//		System.out.println("DateValue Visitor");
	}

	@Override
	public void visit(TimeValue timeValue) {
		setOperand(timeValue.toString());
//		System.out.println("TimeValue Visitor");
	}

	@Override
	public void visit(TimestampValue timestampValue) {
		setOperand(timestampValue.toString());
//		System.out.println("TimestampValue Visitor");
	}

	@Override
	public void visit(StringValue stringValue) {
		setOperand(stringValue.toString());
//		System.out.println("StringValue Visitor");
	}
	
	@Override
	public void visit(Column column) {
		setOperand(column.getWholeColumnName());
	}
	
	@Override
	public void visit(Parenthesis parenthesis) {
		parenthesis.getExpression().accept(this);
	}

	@Override
	public void visit(NullValue arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void visit(Function arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void visit(InverseExpression arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void visit(JdbcParameter arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void visit(Addition arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void visit(Division arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void visit(Multiplication arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void visit(Subtraction arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void visit(AndExpression arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void visit(OrExpression arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void visit(Between arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void visit(InExpression arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void visit(IsNullExpression arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void visit(SubSelect arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void visit(CaseExpression arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void visit(WhenClause arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void visit(ExistsExpression arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void visit(AllComparisonExpression arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void visit(AnyComparisonExpression arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void visit(Concat arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void visit(Matches arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void visit(BitwiseAnd arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void visit(BitwiseOr arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void visit(BitwiseXor arg0) {
		// TODO Auto-generated method stub
		
	}
	
	public Condition getCondition() {
		return condition;
	}
}
